package com.jzy.bxb.manage.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jzy.bxb.core.handler.HttpCode;
import com.jzy.bxb.manage.core.vo.ResultVO;
import com.jzy.bxb.manage.service.SuggestService;
import com.jzy.bxb.manage.service.UserService;
import com.jzy.bxb.model.mongo.entity.Suggest;
import com.jzy.bxb.model.mongo.entity.User;
import com.jzy.bxb.model.util.SMSUtil;

/**
 * 小区
 * @author JiangZhiYong
 * QQ:359135103
 *
 */
@Controller
@RequestMapping("/suggest")
public class SuggestController {
	private static final Logger LOGGER=LoggerFactory.getLogger(SuggestController.class);

	@Autowired
	private SuggestService suggestService;
	@Autowired
	private UserService userService;
	
	/**
	 * 小区列表
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/list")
	public ModelAndView communityList(HttpSession session, HttpServletResponse response){
		try {
			List<Suggest> suggests = suggestService.getSuggests();
			return new ModelAndView("/suggest", "suggests", suggests);
		} catch (Exception e) {
			LOGGER.error("小区",e);
			return new ModelAndView("redirect:/login");
		}
	}
	
	/**
	 * 回复
	 */
	@RequestMapping(value="replay")
	@ResponseBody
	public ResultVO<String> replay(HttpSession session, HttpServletResponse response, @RequestParam long userId, @RequestParam String replayMsg){
		User user = userService.get(userId);
		if(user==null){
			return new ResultVO<String>(HttpCode.ERROR, "提交建议的用户不存在", null);
		}else if(user.getPhone()==null){
			return new ResultVO<String>(HttpCode.ERROR, "用户未绑定手机，发送失败", null);
		}
		SMSUtil.sendSuggestReplay(replayMsg, user.getPhone());	
		return new ResultVO<String>(HttpCode.SUCCESS, "回复成功", null);
	}
}
